package com.jike.noobmoney.mvp.view.fragment.rank;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.TopPushAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.TaskMoneyEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.activity.RankTwoActivity;
import com.jike.noobmoney.mvp.view.activity.RankTwoMouthActivity;
import com.jike.noobmoney.mvp.view.fragment.BaseNewFragment;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TopPushFragment extends BaseNewFragment implements IView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private TopPushAdapter topPushAdapter;
    private TaskPresenter taskPresenter;
    private List<TaskMoneyEntity.TaskweekrankBean> mData = new ArrayList<>();
    private View headerView;
    private String type = "0"; //0是本周/月  1是上周/月
    private String mouth = "0"; //0是周 1是月

    @Override
    protected int layoutResID() {
        return R.layout.fragment_top_push;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        initData();
    }

    protected void initData() {
        taskPresenter = new TaskPresenter(this);

        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getString("type");
            mouth = arguments.getString("mouth");
        }

        headerView = LayoutInflater.from(context).inflate(R.layout.layout_top_push, null, false);
        ImageView ivBnaner = headerView.findViewById(R.id.iv_banner);
        ivBnaner.setVisibility("1".equals(mouth) ? View.GONE : View.VISIBLE);

        TextView viewById = headerView.findViewById(R.id.tv_shangzhou);
        viewById.setVisibility("1".equals(type) ? View.GONE : View.VISIBLE);
        viewById.setText("1".equals(mouth) ? "查看上月排行" : "查看上周排行");
        viewById.setOnClickListener(v -> {
            if ("1".equals(mouth)) {
                Intent intent = new Intent(context, RankTwoMouthActivity.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(context, RankTwoActivity.class);
                startActivity(intent);
            }
        });

        topPushAdapter = new TopPushAdapter(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(topPushAdapter);

        topPushAdapter.setHeaderView(headerView);

        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        showProgressDialog();
        taskPresenter.getPushList(mouth, type, ConstantValue.RequestKey.top_push);
    }


    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgressDialog();
        if (ConstantValue.RequestKey.top_push.equals(actionType)) {
            List<TaskMoneyEntity.TaskweekrankBean> taskmoneylist = (List<TaskMoneyEntity.TaskweekrankBean>) data;
            if (taskmoneylist != null && taskmoneylist.size() > 0) {
                mData.clear();
                mData.addAll(taskmoneylist);
            }
            topPushAdapter.refresh(mData);
        }

    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgressDialog();
        Logger.e("xuke", "msg = " + msg);
    }
}
